home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GSIMAGE.C < prev    next >
C/C++ Source or Header  |  1992-03-25  |  28KB  |  847 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsimage.c */
  21. /* Image procedures for Ghostscript library */
  22. #include "gx.h"
  23. #include "memory_.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxarith.h"
  27. #include "gxmatrix.h"
  28. #include "gspaint.h"
  29. #include "gzstate.h"
  30. #include "gzdevice.h"            /* requires gsstate.h */
  31. #include "gzcolor.h"            /* requires gxdevice.h */
  32. #include "gzpath.h"
  33. #include "gxcpath.h"
  34. #include "gxdevmem.h"
  35. #include "gximage.h"
  36.  
  37. /* Exported size of enumerator */
  38. const uint gs_image_enum_sizeof = sizeof(gs_image_enum);
  39.  
  40. /* Forward declarations */
  41. private int image_init(P9(gs_image_enum *, int, int, int, int, int,
  42.   gs_matrix *, gs_state *, fixed));
  43. /* Procedures for unpacking the input data into 8 bits/sample. */
  44. private void image_unpack_1(iunpack_proc_args);
  45. private void image_unpack_1_spread(iunpack_proc_args);
  46. private void image_unpack_2(iunpack_proc_args);
  47. private void image_unpack_2_spread(iunpack_proc_args);
  48. private void image_unpack_4(iunpack_proc_args);
  49. private void image_unpack_8(iunpack_proc_args);
  50. private void image_unpack_8_spread(iunpack_proc_args);
  51. private void image_unpack_12(iunpack_proc_args);
  52. /* The image_render procedures work on fully expanded, complete rows. */
  53. /* These take a height argument, which is an integer > 0; */
  54. /* they return a negative code, or the number of */
  55. /* rows actually processed (which may be less than the height). */
  56. private int image_render_skip(irender_proc_args);
  57. private int image_render_direct(irender_proc_args);
  58. private int image_render_mono(irender_proc_args);
  59. private int image_render_color(irender_proc_args);
  60.  
  61. /* Set up a gs_color with a transfer-mapped gray sample. */
  62. #define image_set_rgb(rcolor,sample_value)\
  63.   rcolor.luminance = rcolor.red = rcolor.green = rcolor.blue =\
  64.     gx_map_color_param_byte(pgs, sample_value, gray)
  65.  
  66. /* Mask tables for spreading input data. */
  67. /* Note that the mask tables depend on the end-orientation of the CPU. */
  68. /* We can't simply define them as byte arrays, because */
  69. /* they might not wind up properly long- or short-aligned. */
  70. #define map4tox(z,a,b,c,d)\
  71.     z, z^a, z^b, z^(a+b),\
  72.     z^c, z^(a+c), z^(b+c), z^(a+b+c),\
  73.     z^d, z^(a+d), z^(b+d), z^(a+b+d),\
  74.     z^(c+d), z^(a+c+d), z^(b+c+d), z^(a+b+c+d)
  75. #if arch_is_big_endian
  76. private unsigned long map_4_to_32[16] =
  77.    {    map4tox(0L, 0xffL, 0xff00L, 0xff0000L, 0xff000000L)    };
  78. private unsigned long map_4_to_32_invert[16] =
  79.    {    map4tox(0xffffffffL, 0xffL, 0xff00L, 0xff0000L, 0xff000000L)    };
  80. private unsigned short map_4_to_16[16] =
  81.    {    map4tox(0, 0x55, 0xaa, 0x5500, 0xaa00)    };
  82. #else                    /* !arch_is_big_endian */
  83. private unsigned long map_4_to_32[16] =
  84.    {    map4tox(0L, 0xff000000L, 0xff0000L, 0xff00L, 0xffL)    };
  85. private unsigned long map_4_to_32_invert[16] =
  86.    {    map4tox(0xffffffffL, 0xff000000L, 0xff0000L, 0xff00L, 0xffL)    };
  87. private unsigned short map_4_to_16[16] =
  88.    {    map4tox(0, 0x5500, 0xaa00, 0x55, 0xaa)    };
  89. #endif
  90.  
  91. /* Start processing an image */
  92. int
  93. gs_image_init(gs_image_enum *penum, gs_state *pgs,
  94.   int width, int height, int bps, int spp, gs_matrix *pmat)
  95. {    int spread;
  96.     if ( pgs->in_cachedevice ) return_error(gs_error_undefined);
  97.     switch ( spp )
  98.        {
  99.     case 1: case 3: case 4:
  100.         spread = 1; break;
  101.     case -3: case -4:
  102.         spp = -spp; spread = spp; break;
  103.     default:
  104.         return_error(gs_error_rangecheck);
  105.        }
  106.     if ( spp == 1 )
  107.        {    /* Initialize the color table */
  108. #define chtl(i)\
  109.   penum->dev_colors[i].halftone_level
  110.         switch ( bps )
  111.            {
  112.         default:
  113.            {    /* Yes, clearing the entire table is slow, */
  114.             /* but for 8 bit-per-sample images, it's worth it. */
  115.             register gx_device_color *pcht = &penum->dev_colors[0];
  116.             register int n = 64;
  117.             do
  118.                {    pcht[0].halftone_level =
  119.                   pcht[1].halftone_level =
  120.                   pcht[2].halftone_level =
  121.                   pcht[3].halftone_level = -1;
  122.                 pcht += 4;
  123.                }
  124.             while ( --n > 0 );
  125.             break;
  126.            }
  127.         case 4:
  128.             chtl(17) = chtl(2*17) = chtl(3*17) =
  129.               chtl(4*17) = chtl(6*17) = chtl(7*17) =
  130.               chtl(8*17) = chtl(9*17) = chtl(11*17) =
  131.               chtl(12*17) = chtl(13*17) = chtl(14*17) = -1;
  132.             /* falls through */
  133.         case 2:
  134.             chtl(5*17) = chtl(10*17) = -1;
  135.         case 1:
  136.             ;
  137.            }
  138.         /* Pre-map entries 0 and 255. */
  139.            {    gs_color rcolor;
  140.             image_set_rgb(rcolor, 0);
  141.             gx_color_render(&rcolor, &penum->icolor0, pgs);
  142.             image_set_rgb(rcolor, 255);
  143.             gx_color_render(&rcolor, &penum->icolor1, pgs);
  144.            }
  145. #undef chtl
  146.        }
  147.     penum->masked = 0;
  148.     penum->map4to32 = map_4_to_32;
  149.     return image_init(penum, width, height, bps, spp, spread,
  150.               pmat, pgs, (fixed)0);
  151. }
  152.  
  153. /* Start processing a masked image */
  154. int
  155. gs_imagemask_init(gs_image_enum *penum, gs_state *pgs,
  156.   int width, int height, int invert, gs_matrix *pmat, int adjust)
  157. {    /* Initialize color entries 0 and 255. */
  158.     penum->icolor0.halftone_level = 0;
  159.     penum->icolor0.color1 = penum->icolor0.color2 = gx_no_color_index;
  160.     penum->icolor1 = *pgs->dev_color;
  161.     penum->masked = 1;
  162.     penum->map4to32 = (invert ? map_4_to_32_invert : map_4_to_32);
  163.     return image_init(penum, width, height, 1, 1, 1, pmat, pgs,
  164.               (adjust && pgs->in_cachedevice ?
  165.                float2fixed(0.25) : (fixed)0));
  166. }
  167.  
  168. /* Common setup for image and imagemask. */
  169. /* Caller has set penum->masked, map4to32, dev_colors[]. */
  170. private int
  171. image_init(register gs_image_enum *penum, int width, int height,
  172.   int bps, int spp, int spread, gs_matrix *pmat, gs_state *pgs,
  173.   fixed adjust)
  174. {    int code;
  175.     int index_bps;
  176.     gs_matrix mat;
  177.     gs_fixed_rect clip_box;
  178.     uint bsize = (width + 8) * spp;    /* round up, +1 for end-of-run byte */
  179.     byte *buffer;
  180.     fixed mtx, mty;
  181.     if ( width <= 0 || height < 0 )
  182.         return_error(gs_error_rangecheck);
  183.     switch ( bps )
  184.        {
  185.     case 1: index_bps = 0; break;
  186.     case 2: index_bps = 1; break;
  187.     case 4: index_bps = 2; break;
  188.     case 8: index_bps = 3; break;
  189.     case 12: index_bps = 4; break;
  190.     default: return_error(gs_error_rangecheck);
  191.        }
  192.     if ( height == 0 ) return 0;    /* empty image */
  193.     if (    (code = gs_matrix_invert(pmat, &mat)) < 0 ||
  194.         (code = gs_matrix_multiply(&mat, &ctm_only(pgs), &mat)) < 0
  195.        )    return code;
  196.     buffer = (byte *)gs_malloc(1, bsize, "image buffer");
  197.     if ( buffer == 0 ) return_error(gs_error_VMerror);
  198.     penum->width = width;
  199.     penum->height = height;
  200.     penum->bps = bps;
  201.     penum->spp = spp;
  202.     penum->spread = spread;
  203.     penum->fxx = float2fixed(mat.xx);
  204.     penum->fyy = float2fixed(mat.yy);
  205.     if ( (penum->skewed = is_skewed(&mat)) )
  206.        {    penum->fxy = float2fixed(mat.xy);
  207.         penum->fyx = float2fixed(mat.yx);
  208.        }
  209.     else
  210.        {    penum->fxy = 0;
  211.         penum->fyx = 0;
  212.        }
  213.     penum->xcur = mtx = float2fixed(mat.tx);
  214.     penum->ycur = mty = float2fixed(mat.ty);
  215.     penum->pgs = pgs;
  216.     clip_box = pgs->clip_path->path.bbox;    /* box is known to be up to date */
  217.     penum->clip_box = clip_box;
  218.     penum->buffer = buffer;
  219.     penum->buffer_size = bsize;
  220.     penum->bytes_per_row =
  221.         (uint)(((ulong)width * (bps * spp) / spread + 7) >> 3);
  222.     penum->slow_loop = penum->skewed;
  223.     /* If all four extrema of the image fall within the clipping */
  224.     /* rectangle, clipping is never required. */
  225.        {    gs_fixed_rect cbox;
  226.         fixed edx = float2fixed(mat.xx * width);
  227.         fixed edy = float2fixed(mat.yy * height);
  228.         fixed epx, epy, eqx, eqy;
  229.         if ( edx < 0 ) epx = edx, eqx = 0;
  230.         else epx = 0, eqx = edx;
  231.         if ( edy < 0 ) epy = edy, eqy = 0;
  232.         else epy = 0, eqy = edy;
  233.         if ( penum->skewed )
  234.            {    edx = float2fixed(mat.yx * height);
  235.             edy = float2fixed(mat.xy * width);
  236.             if ( edx < 0 ) epx += edx; else eqx += edx;
  237.             if ( edy < 0 ) epy += edy; else eqy += edy;
  238.            }
  239.         gx_cpath_box_for_check(pgs->clip_path, &cbox);
  240.         penum->never_clip =
  241.             mtx + epx >= cbox.p.x && mtx + eqx <= cbox.q.x &&
  242.             mty + epy >= cbox.p.y && mty + eqy <= cbox.q.y;
  243. #ifdef DEBUG
  244. if ( gs_debug['b'] | gs_debug['B'] )
  245.     dprintf4("[b]Image: cbox=(%g,%g),(%g,%g)\n",
  246.          fixed2float(cbox.p.x), fixed2float(cbox.p.y),
  247.          fixed2float(cbox.q.x), fixed2float(cbox.q.y)),
  248.     dprintf3("     mt=(%g,%g) never_clip=%d\n",
  249.          fixed2float(mtx), fixed2float(mty), penum->never_clip);
  250. #endif
  251.        }
  252.        {    static void (*procs[5])(iunpack_proc_args) = {
  253.             image_unpack_1, image_unpack_2,
  254.             image_unpack_4, image_unpack_8, image_unpack_12
  255.            };
  256.         static void (*spread_procs[5])(iunpack_proc_args) = {
  257.             image_unpack_1_spread, image_unpack_2_spread,
  258.             image_unpack_4, image_unpack_8_spread,
  259.             image_unpack_12
  260.            };
  261.         penum->slow_loop |=
  262.             /* Use slow loop for imagemask with a halftone */
  263.             (penum->masked &&
  264.              !color_is_pure(pgs->dev_color));
  265.         if ( pgs->in_charpath )
  266.             penum->render = image_render_skip;
  267.         else if ( spp == 1 && bps == 1 && !penum->slow_loop &&
  268.              (fixed2long_rounded(mtx + width * penum->fxx) -
  269.               fixed2long_rounded(mtx) == width) &&
  270.               (penum->masked || color_is_pure(&penum->icolor0))
  271.            )
  272.             penum->render = image_render_direct;
  273.         else
  274.             penum->render =
  275.               (spp == 1 ? image_render_mono : image_render_color);
  276.         /* If the image is 1-for-1 with the device, */
  277.         /* we don't want to spread the samples. */
  278.         /* The following should just be an assignment of */
  279.         /* a conditional expression, but the Ultrix C compiler */
  280.         /* can't handle it. */
  281.         if ( penum->render == image_render_direct )
  282.           penum->unpack = image_unpack_8;
  283.         else if ( spread != 1 )
  284.           penum->unpack = spread_procs[index_bps];
  285.         else
  286.           penum->unpack = procs[index_bps];
  287.        }
  288.     if ( !penum->never_clip )
  289.        {    /* Set up the clipping device. */
  290.         gx_device *dev = (gx_device *)&penum->clip_dev;
  291.         penum->clip_dev = gs_clip_device;
  292.         penum->clip_dev.target = gs_currentdevice(pgs);
  293.         penum->clip_dev.list = pgs->clip_path->list;
  294.         (*dev->procs->open_device)(dev);
  295.        }
  296.     penum->adjust = adjust;
  297.     penum->plane_index = 0;
  298.     penum->byte_in_row = 0;
  299.     penum->y = 0;
  300. #ifdef DEBUG
  301. if ( gs_debug['b'] | gs_debug['B'] )
  302.     dprintf3("[b]Image: w=%d h=%d %s\n",
  303.          width, height, (penum->never_clip ? "no clip" : "must clip")),
  304.     dprintf6("   [%f %f %f %f %f %f]\n",
  305.          mat.xx, mat.xy, mat.yx, mat.yy, mat.tx, mat.ty);
  306. #endif
  307.     return 0;
  308. }
  309.  
  310. /* Process the next piece of an image */
  311. int
  312. gs_image_next(register gs_image_enum *penum, byte *dbytes, uint dsize)
  313. {    uint rsize = penum->bytes_per_row;
  314.     uint pos = penum->byte_in_row;
  315.     int width = penum->width;
  316.     uint dleft = dsize;
  317.     uint dpos = 0;
  318.     gs_state *pgs = penum->pgs;
  319.     gx_device *save_dev = 0;
  320.     int code;
  321.     /* Accumulate separated colors, if needed */
  322.     if ( penum->plane_index == 0 )
  323.         penum->plane_size = dsize;
  324.     else if ( dsize != penum->plane_size )
  325.         return_error(gs_error_undefinedresult);
  326.     penum->planes[penum->plane_index] = dbytes;
  327.     if ( ++(penum->plane_index) != penum->spread )
  328.         return 0;
  329.     penum->plane_index = 0;
  330.     /* We've accumulated an entire set of planes. */
  331.     if ( !penum->never_clip )
  332.        {    /* Install the clipping device if needed. */
  333.         gx_device *dev = (gx_device *)&penum->clip_dev;
  334.         save_dev = gs_currentdevice(pgs);
  335.         penum->clip_dev.target = save_dev;
  336.         gx_set_device_only(pgs, dev);
  337.        }
  338.     while ( dleft )
  339.        {    /* Fill up a row, then display it. */
  340.         uint bcount = min(dleft, rsize - pos);
  341.         byte *bptr =
  342.           penum->buffer + (pos << 3)  / penum->bps * penum->spread;
  343.         int px;
  344.         for ( px = 0; px < penum->spread; px++ )
  345.           (*penum->unpack)(penum, bptr + px, penum->planes[px] + dpos, bcount, pos);
  346.         pos += bcount;
  347.         dpos += bcount;
  348.         dleft -= bcount;
  349.         if ( pos == rsize )    /* filled an entire row */
  350.            {
  351. #ifdef DEBUG
  352. if ( gs_debug['B'] )
  353.    {            int i, n = width * penum->spp;
  354.             dputs("[B]row:");
  355.             for ( i = 0; i < n; i++ )
  356.                 dprintf1(" %02x", penum->buffer[i]);
  357.             dputs("\n");
  358.    }
  359. #endif
  360.             if ( !penum->skewed )
  361.               { /* Precompute integer y and height, */
  362.                 /* and check for clipping. */
  363.                 fixed yc = penum->ycur, yn;
  364.                 fixed dyy = penum->fyy;
  365.                 fixed adjust = penum->adjust;
  366.                 if ( dyy > 0 )
  367.                   dyy += adjust << 1,
  368.                   yc -= adjust;
  369.                 else
  370.                   dyy = (adjust << 1) - dyy,
  371.                   yc -= dyy - adjust;
  372.                 if ( yc >= penum->clip_box.q.y ) goto mt;
  373.                 yn = yc + dyy;
  374.                 if ( yn <= penum->clip_box.p.y ) goto mt;
  375.                 penum->yci = fixed2int_var_rounded(yc);
  376.                 penum->hci =
  377.                   fixed2int_var_rounded(yn) - penum->yci;
  378.                 if ( penum->hci == 0 ) goto mt;
  379.               }
  380.             code = (*penum->render)(penum, penum->buffer, width * penum->spp, 1);
  381.             if ( code < 0 ) goto err;
  382. mt:            if ( ++(penum->y) == penum->height ) goto end;
  383.             pos = 0;
  384.             penum->xcur += penum->fyx;
  385.             penum->ycur += penum->fyy;
  386.            }
  387.        }
  388.     penum->byte_in_row = pos;
  389.     code = 0;
  390.     goto out;
  391. end:    /* End of data */
  392.     code = 1;
  393.     /* falls through */
  394. err:    /* Error, abort */
  395.     gs_free((char *)penum->buffer, penum->buffer_size, 1, "image buffer");
  396. out:    if ( save_dev != 0 ) gx_set_device_only(pgs, save_dev);
  397.     return code;
  398. }
  399.  
  400. /* ------ Unpacking procedures ------ */
  401.  
  402. private void
  403. image_unpack_1(gs_image_enum *penum, byte *bptr,
  404.   register byte *data, uint dsize, uint inpos)
  405. {    register unsigned long *bufp = (unsigned long *)bptr;
  406.     int left = dsize;
  407.     register unsigned long _ds *map = penum->map4to32; /* may invert */
  408.     while ( left-- )
  409.        {    register unsigned b = *data++;
  410.         *bufp++ = map[b >> 4];
  411.         *bufp++ = map[b & 0xf];
  412.        }
  413. }
  414.  
  415. private void
  416. image_unpack_1_spread(gs_image_enum *penum, register byte *bufp,
  417.   register byte *data, uint dsize, uint inpos)
  418. {    register int spread = penum->spread;
  419.     int left = dsize;
  420.     while ( left-- )
  421.        {    register unsigned b = *data++;
  422.         *bufp = -(b >> 7); bufp += spread;
  423.         *bufp = -((b >> 6) & 1); bufp += spread;
  424.         *bufp = -((b >> 5) & 1); bufp += spread;
  425.         *bufp = -((b >> 4) & 1); bufp += spread;
  426.         *bufp = -((b >> 3) & 1); bufp += spread;
  427.         *bufp = -((b >> 2) & 1); bufp += spread;
  428.         *bufp = -((b >> 1) & 1); bufp += spread;
  429.         *bufp = -(b & 1); bufp += spread;
  430.        }
  431. }
  432.  
  433. private void
  434. image_unpack_2(gs_image_enum *penum, byte *bptr,
  435.   register byte *data, uint dsize, uint inpos)
  436. {    register unsigned short *bufp = (unsigned short *)bptr;
  437.     int left = dsize;
  438.     register unsigned short _ds *map = map_4_to_16;
  439.     while ( left-- )
  440.        {    register unsigned b = *data++;
  441.         *bufp++ = map[b >> 4];
  442.         *bufp++ = map[b & 0xf];
  443.        }
  444. }
  445.  
  446. private void
  447. image_unpack_2_spread(gs_image_enum *penum, register byte *bufp,
  448.   register byte *data, uint dsize, uint inpos)
  449. {    register int spread = penum->spread;
  450.     int left = dsize;
  451.     static byte map_2_to_8[4] = { 0, 0x55, 0xaa, 0xff };
  452.     register byte _ds *map = map_2_to_8;
  453.     while ( left-- )
  454.        {    register unsigned b = *data++;
  455.         *bufp = map[b >> 6]; bufp += spread;
  456.         *bufp = map[(b >> 4) & 3]; bufp += spread;
  457.         *bufp = map[(b >> 2) & 3]; bufp += spread;
  458.         *bufp = map[b & 3]; bufp += spread;
  459.        }
  460. }
  461.  
  462. private void
  463. image_unpack_4(gs_image_enum *penum, register byte *bufp,
  464.   register byte *data, uint dsize, uint inpos)
  465. {    register int spread = penum->spread;
  466.     int left = dsize;
  467.     while ( left-- )
  468.        {    register unsigned b = *data++;
  469.         *bufp = (b & 0xf0) + (b >> 4); bufp += spread;
  470.         b &= 0xf;
  471.         *bufp = (b << 4) + b; bufp += spread;
  472.        }
  473. }
  474.  
  475. private void
  476. image_unpack_8(gs_image_enum *penum, byte *bufp,
  477.   byte *data, uint dsize, uint inpos)
  478. {    if ( data != bufp ) memcpy(bufp, data, dsize);
  479. }
  480.  
  481. private void
  482. image_unpack_8_spread(gs_image_enum *penum, register byte *bufp,
  483.   register byte *data, uint dsize, uint inpos)
  484. {    register int spread = penum->spread;
  485.     register int left = dsize;
  486.     while ( left-- )
  487.        {    *bufp = *data++; bufp += spread;
  488.        }
  489. }
  490.  
  491. private void
  492. image_unpack_12(gs_image_enum *penum, register byte *bufp,
  493.   register byte *data, uint dsize, uint inpos)
  494. {    register int spread = penum->spread;
  495.     register int left = dsize;
  496.     /* We have to deal with the 3 cases of inpos % 3 separately. */
  497.     /* (In fact, this is the only reason inpos is passed to */
  498.     /* the unpacking procedures at all.) */
  499.     /* Let N = inpos / 3. */
  500.     switch ( inpos % 3 )
  501.        {
  502.     case 1:
  503.         /* bufp points to byte 2N, which was already filled */
  504.         /* with the leftover byte from the previous call. */
  505.         bufp += spread;
  506.         *bufp = *data++ << 4;
  507.         if ( !--left ) return;
  508.     case 2:
  509.         /* bufp points to byte 2N+1, which was half-filled */
  510.         /* with the second leftover byte from the previous call. */
  511.         *bufp += *data++ >> 4;
  512.         --left;
  513.     case 0:
  514.         /* Nothing special to do. */
  515.         ;
  516.        }
  517.     /* Just drop the low 4 bits of each 12. */
  518.     while ( left >= 3 )
  519.        {    *bufp = *data;
  520.         bufp += spread;
  521.         *bufp = (data[1] << 4) + (data[2] >> 4);
  522.         bufp += spread;
  523.         data += 3;
  524.        }
  525.     switch ( left )
  526.        {
  527.     case 2:                /* dddddddd xxxxdddd */
  528.         bufp[1] = data[1] << 4;
  529.     case 1:                /* dddddddd */
  530.         *bufp = *data;
  531.     case 0:                /* Nothing more to do. */
  532.         ;
  533.        }
  534. }
  535.  
  536. /* ------ Rendering procedures ------ */
  537.  
  538. /* Rendering procedure for ignoring an image.  We still need to iterate */
  539. /* over the samples, because the procedure might have side effects. */
  540. private int
  541. image_render_skip(gs_image_enum *penum, byte *data, uint w, int h)
  542. {    return h;
  543. }
  544.  
  545. /* Rendering procedure for a 1-bit-per-pixel sampled image */
  546. /* with no skewing/rotation or X scaling. */
  547. /* In this case a direct BitBlt is possible. */
  548. private int
  549. image_render_direct(register gs_image_enum *penum, byte *data, uint w, int h)
  550. {    int ix = fixed2int_var_rounded(penum->xcur), iw = w;
  551.     int iy = penum->yci, ih = penum->hci;
  552.     uint raster = (w + 7) >> 3;
  553.     gx_device *dev = penum->pgs->device->info;
  554.     dev_proc_copy_mono((*copy_mono)) = dev->procs->copy_mono;
  555.     gx_color_index
  556.         zero = penum->icolor0.color1,
  557.         one = penum->icolor1.color1;
  558. #ifdef DEBUG
  559. if ( gs_debug['b'] | gs_debug['B'] )
  560.     dprintf4("[b]direct (%d,%d),(%d,%d)\n", ix, iy, iw, ih);
  561. #endif
  562.     if ( ih == 1 && h == 1 && penum->fyy >= 0 )
  563.        {    /* We can do the whole thing at once. */
  564.         (*copy_mono)(dev, data, 0, raster, gx_no_bitmap_id,
  565.             ix, iy, iw, ih, zero, one);
  566.         return h;
  567.        }
  568.     else
  569.        {    /* Do just one row, clipping if necessary. */
  570.         int dy;
  571.         for ( dy = 0; dy < ih; dy++ )
  572.             (*copy_mono)(dev, data, 0, raster, gx_no_bitmap_id,
  573.                 ix, iy + dy, iw, 1, zero, one);
  574.         return 1;
  575.        }
  576. }
  577.  
  578. /* Rendering procedure for the general case of displaying a */
  579. /* monochrome image, dealing with multiple bit-per-sample images, */
  580. /* bits not 1-for-1 with the device, and general transformations. */
  581. /* This procedure handles a single scan line. */
  582. private int
  583. image_render_mono(gs_image_enum *penum, byte *buffer, uint w, int h)
  584. {    gs_state *pgs = penum->pgs;
  585.     const int masked = penum->masked;
  586.     const fixed dxx = penum->fxx;
  587.     fixed xt = penum->xcur;
  588.     gs_color rcolor;
  589.     gx_device_color *pdevc = pgs->dev_color;
  590.     /* Make sure the cache setup matches the graphics state. */
  591.     /* Also determine whether all tiles fit in the cache. */
  592.     int tiles_fit = gx_check_tile_cache(pgs);
  593. #define image_set_gray(sample_value)\
  594.    { pdevc = &penum->dev_colors[sample_value];\
  595.      switch ( pdevc->halftone_level )\
  596.       { default:        /* halftone */\
  597.       if ( !tiles_fit ) gx_color_load(pdevc, pgs); break;\
  598.         case -1:        /* not computed yet */\
  599.       image_set_rgb(rcolor, sample_value);\
  600.       gx_color_render(&rcolor, pdevc, pgs);\
  601.     case 0: ;        /* pure color */\
  602.       }\
  603.    }
  604.     fixed xl = xt;
  605.     byte *psrc = buffer;
  606.     byte *endp = buffer + w;
  607.     fixed xrun = xt;        /* x at start of run */
  608.     int run = *psrc;        /* run value */
  609.     int htrun =            /* halftone run value */
  610.       (masked ? 255 : -2);
  611.     *endp = ~endp[-1];    /* force end of run */
  612.     gx_set_gray_only(&rcolor, (color_param)0);
  613.     if ( penum->slow_loop )
  614.       { /* Skewed, or imagemask with a halftone. */
  615.         const fixed
  616.           dxy = penum->fxy, dyx = penum->fyx,
  617.           dyy = penum->fyy;
  618.         fixed ytf = penum->ycur;
  619.         fixed yrun = ytf;
  620.         for ( ; ; )
  621.           { if ( *psrc++ != run )
  622.           { /* Fill the region between xrun and xl. */
  623.             if ( run != htrun )
  624.               { if ( run == 0 )
  625.               { if ( masked ) goto trans;
  626.               }
  627.             htrun = run;
  628.             image_set_gray(run);
  629.               }
  630.             gz_fill_pgram_fixed(xrun, yrun, xl - xrun,
  631.                     ytf - yrun, dyx, dyy,
  632.                     pdevc, pgs);
  633. trans:            if ( psrc > endp ) break;
  634.             yrun = ytf;
  635.             xrun = xl;
  636.             run = psrc[-1];
  637.           }
  638.         xl += dxx;
  639.         ytf += dxy;        /* harmless if no skew */
  640.           }
  641.       }
  642.     else            /* fast loop */
  643.       { /* No skew, and not imagemask with a halftone. */
  644.         const fixed adjust = penum->adjust;
  645.         fixed xa = (dxx >= 0 ? adjust : -adjust);
  646.         const int yt = penum->yci, iht = penum->hci;
  647.         gx_device *dev = pgs->device->info;
  648.         dev_proc_fill_rectangle((*fill_proc)) = dev->procs->fill_rectangle;
  649.         dev_proc_tile_rectangle((*tile_proc)) = dev->procs->tile_rectangle;
  650.         dev_proc_copy_mono((*copy_mono_proc)) = dev->procs->copy_mono;
  651.         dev_proc_copy_color((*copy_color_proc)) = dev->procs->copy_color;
  652.         /* If each pixel is likely to fit in a single halftone tile, */
  653.         /* determine that now (tile_offset = offset of row within tile). */
  654.         int tile_offset =
  655.           gx_check_tile_size(pgs,
  656.                  fixed2int_rounded(any_abs(dxx) + (xa << 1)),
  657.                  yt, iht);
  658.         /* Fold the adjustment into xrun and xl, */
  659.         /* including the +0.5 for rounding. */
  660.         xrun = xrun - xa + float2fixed(0.5);
  661.         xl = xl + xa + float2fixed(0.5);
  662.         xa <<= 1;
  663.         for ( ; ; )
  664.           { if ( *psrc++ != run )
  665.           { /* Fill the region between xrun and xl. */
  666.             int xi = fixed2int_var(xrun);
  667.             int wi = fixed2int_var(xl) - xi;
  668.             int tsx, code;
  669.             gx_bitmap *tile;
  670.             if ( wi <= 0 )
  671.               { if ( wi == 0 ) goto mt;
  672.             xi += wi, wi = -wi;
  673.               }
  674.             switch ( run )
  675.               {
  676.               case 0:
  677.             if ( masked ) goto mt;
  678.             if ( !color_is_pure(&penum->icolor0) ) goto ht;
  679.             code = (*fill_proc)(dev, xi, yt, wi, iht, penum->icolor0.color1);
  680.             break;
  681.               case 255:        /* just for speed */
  682.             if ( !color_is_pure(&penum->icolor1) ) goto ht;
  683.             code = (*fill_proc)(dev, xi, yt, wi, iht, penum->icolor1.color1);
  684.             break;
  685.               default:
  686. ht:            /* Use halftone if needed */
  687.             if ( run != htrun )
  688.               { image_set_gray(run);
  689.                 htrun = run;
  690.               }
  691.             /* We open-code gz_fill_rectangle_open, */
  692.             /* because we've done some of the work for */
  693.             /* halftone tiles in advance. */
  694.             if ( color_is_pure(pdevc) )
  695.               { code = (*fill_proc)(dev, xi, yt, wi, iht, pdevc->color1);
  696.               }
  697.             else if ( tile_offset >= 0 &&
  698.                   (tile = pdevc->tile,
  699.                    (tsx = (xi + pgs->phase_mod.x) % tile->rep_width) + wi <= tile->size.x)
  700.                 )
  701.               { /* The pixel(s) fit(s) in a single tile. */
  702.                 byte *row = tile->data + tile_offset;
  703.                 code = (color_is_color_halftone(pdevc) ?
  704.                     (*copy_color_proc)
  705.                       (dev, row, tsx, tile->raster, gx_no_bitmap_id,
  706.                        xi, yt, wi, iht) :
  707.                     (*copy_mono_proc)
  708.                       (dev, row, tsx, tile->raster, gx_no_bitmap_id,
  709.                        xi, yt, wi, iht,
  710.                        pdevc->color1, pdevc->color2)
  711.                     );
  712.               }
  713.             else
  714.               { code = (*tile_proc)(dev, pdevc->tile, xi, yt, wi, iht,
  715.                          pdevc->color1, pdevc->color2,
  716.                          pgs->phase_mod.x, pgs->phase_mod.y);
  717.               }
  718.               }
  719.             if ( code < 0 ) return code;
  720. mt:            if ( psrc > endp ) break;
  721.             xrun = xl - xa;    /* original xa << 1 */
  722.             run = psrc[-1];
  723.           }
  724.         xl += dxx;
  725.           }
  726.       }
  727.     return 1;
  728. }
  729.  
  730. /* Rendering procedure for handling color images. */
  731. typedef union { struct { byte r, g, b, skip; } v; ulong all; } color_sample;
  732. private int
  733. image_render_color(gs_image_enum *penum, byte *buffer, uint w, int h)
  734. {    gs_state *pgs = penum->pgs;
  735.     fixed    dxx = penum->fxx, dxy = penum->fxy,
  736.         dyx = penum->fyx, dyy = penum->fyy;
  737.     int skew = penum->skewed;
  738.     fixed xt = penum->xcur;
  739.     fixed ytf = penum->ycur;
  740.     int yt = penum->yci, iht = penum->hci;
  741.     gs_color rcolor;
  742.     gx_device_color devc1, devc2;
  743.     gx_device_color _ss *spdevc = &devc1;
  744.     gx_device_color _ss *spdevc_next = &devc2;
  745. #define pdevc (gx_device_color *)spdevc
  746. #define pdevc_next (gx_device_color *)spdevc_next
  747.     int spp = penum->spp;
  748.     fixed xl = xt;
  749.     byte *psrc = buffer;
  750.     fixed xrun = xt;        /* x at start of run */
  751.     int irun = fixed2int_var_rounded(xrun);    /* int xrun */
  752.     fixed yrun = ytf;        /* y ditto */
  753.     color_sample run;        /* run value */
  754.     color_sample next;        /* next sample value */
  755.     byte *bufend = buffer + w;
  756.     bufend[0] = ~bufend[-spp];    /* force end of run */
  757. #ifdef DEBUG
  758. if ( gs_debug['b'] | gs_debug['B'] )
  759.     dprintf5("[b]y=%d w=%d xt=%f yt=%f yb=%f\n",
  760.          penum->y, w,
  761.          fixed2float(xt), fixed2float(ytf), fixed2float(ytf + dyy));
  762. #endif
  763.     run.all = 0;
  764.     next.all = 0;
  765.     rcolor.red = rcolor.green = rcolor.blue = 0;
  766.     gx_color_from_rgb(&rcolor);
  767.     gx_color_render(&rcolor, pdevc, pgs);
  768.     while ( psrc <= bufend )    /* 1 extra iteration */
  769.                 /* to handle final run */
  770.        {    if ( spp == 4 )        /* cmyk */
  771.            {    switch ( psrc[3] )
  772.                {
  773.             case 0:        /* no black */
  774.                 next.v.r = ~psrc[0];
  775.                 next.v.g = ~psrc[1];
  776.                 next.v.b = ~psrc[2];
  777.                 break;
  778.             case 0xff:    /* all black */
  779.                 next.v.r = next.v.g = next.v.b = 0;
  780.                 break;
  781.             default:
  782.                {    uint black = 0xff - psrc[3];
  783.                 /* The following is equivalent to */
  784.                 /* v * black / 0xff, without the divide. */
  785.                 register uint temp;
  786. #define deduct_black(v)\
  787.   (temp = (v) * black, (temp + (temp >> 8) + 1) >> 8)
  788.                 next.v.r = deduct_black(0xff - psrc[0]);
  789.                 next.v.g = deduct_black(0xff - psrc[1]);
  790.                 next.v.b = deduct_black(0xff - psrc[2]);
  791. #undef deduct_black
  792.                }
  793.                }
  794.             psrc += 4;
  795.            }
  796.         else            /* rgb */
  797.            {    next.v.r = psrc[0];
  798.             next.v.g = psrc[1];
  799.             next.v.b = psrc[2];
  800.             psrc += 3;
  801.            }
  802.         if ( next.all != run.all )
  803.            {    rcolor.red = gx_map_color_param_byte(pgs, next.v.r, red);
  804.             rcolor.green = gx_map_color_param_byte(pgs, next.v.g, green);
  805.             rcolor.blue = gx_map_color_param_byte(pgs, next.v.b, blue);
  806.             gx_color_from_rgb(&rcolor);
  807.             gx_color_render(&rcolor, pdevc_next, pgs);
  808.             /* Even though the supplied colors don't match, */
  809.             /* the device colors might. */
  810.             if ( devc1.color1 != devc2.color1 ||
  811.                  devc1.halftone_level != devc2.halftone_level ||
  812.                  (devc1.halftone_level &&
  813.                   devc1.color2 != devc2.color2) ||
  814.                  psrc > bufend    /* force end of last run */
  815.                )
  816.                {    /* Fill the region between */
  817.                 /* xrun/irun and xl */
  818.                 gx_device_color _ss *sptemp;
  819.                 int code;
  820.                 if ( skew )
  821.                {    /* Parallelogram */
  822.                 code = gz_fill_pgram_fixed(xrun, yrun,
  823.                     xl - xrun, ytf - yrun, dyx, dyy,
  824.                     pdevc, pgs);
  825.                 xrun = xl;
  826.                 yrun = ytf;
  827.                }
  828.                 else
  829.                {    /* Rectangle */
  830.                 int xi = irun;
  831.                 int wi = (irun = fixed2int_var_rounded(xl)) - xi;
  832.                 if ( wi < 0 ) xi += wi, wi = -wi;
  833.                 code = gz_fill_rectangle(xi, yt, wi, iht, pdevc, pgs);
  834.                }
  835.                 if ( code < 0 ) return code;
  836.                 sptemp = spdevc;
  837.                 spdevc = spdevc_next;
  838.                 spdevc_next = sptemp;
  839.                }
  840.             run.all = next.all;
  841.            }
  842.         xl += dxx;
  843.         ytf += dxy;        /* harmless if no skew */
  844.        }
  845.     return 1;
  846. }
  847.